package com.nkdx.ws.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.nkdx.ws.dto.UserDto;
import com.nkdx.ws.dto.UserLogDto;
import com.nkdx.ws.eto.UserLogEto;
import com.nkdx.ws.mapper.UserLogEtoMapper;
import com.nkdx.ws.service.UserLogService;
import com.nkdx.ws.util.Utils;
import org.aspectj.lang.JoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 *
 *  用户日志
 * @author ZZW
 * @date 2022/3/20 8:49 上午
 * @param
 * @return
 */
@Service
public class UserLogServiceImpl implements UserLogService {

    private HttpServletRequest request;

    private UserLogEtoMapper userLogEtoMapper;

    @Autowired
    public UserLogServiceImpl (HttpServletRequest request,UserLogEtoMapper userLogEtoMapper){
        this.request = request;
        this.userLogEtoMapper = userLogEtoMapper;
    }


    @Override
    public void addUserLog(JoinPoint joinPoint, String methodName, String type, String description) {
        UserLogEto userLog = new UserLogEto();
        UserDto user=(UserDto) request.getSession().getAttribute("user");
        String url = request.getRequestURL().toString();
        if (user != null){
            userLog.setUserId(user.getId());
            userLog.setIp(Utils.getIpAdrress(request));
            userLog.setCreateTime(new Date());
            userLog.setParameter(JSONObject.toJSONString(request.getParameterMap()));
            userLog.setType(type);
            userLog.setDesc(description);
            userLog.setUrl(url);
            userLogEtoMapper.insert(userLog);
        }
    }

    @Override
    public List<UserLogDto> findAll() {
        return userLogEtoMapper.findAll();
    }
}
